package rpc.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/*********
 *
 *
 *@author 197
 *@date 2020/6/19 17:03
 *
 **/


public class DateUtils {

  private static final ThreadLocal<SimpleDateFormat> SIMPLE_DATE_FORMAT_THREAD_LOCAL = new ThreadLocal<SimpleDateFormat>() {
    @Override
    public SimpleDateFormat get() {
      return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }
  };
  private static final ThreadLocal<SimpleDateFormat> SIMPLE_DATE_FORMAT_THREAD_LOCAL1 = new ThreadLocal<SimpleDateFormat>() {
    @Override
    public SimpleDateFormat get() {
      return new SimpleDateFormat("yyyy-MM-dd");
    }
  };

  /****
   *
   * @return "yyyy-MM-dd"
   */
  public static ThreadLocal<SimpleDateFormat> getSimpleDateFormatThreadLocal1() {
    return SIMPLE_DATE_FORMAT_THREAD_LOCAL1;
  }

  /***
   *
   * @param date
   * @return yyyy-MM-dd HH:mm:ss
   */
  public static String format(Date date) {
    return DateUtils.getSimpleDateFormatThreadLocal().get().format(date);
  }

  /***
   *
   * @return  "yyyy-MM-dd HH:mm:ss"
   */
  public static ThreadLocal<SimpleDateFormat> getSimpleDateFormatThreadLocal() {
    return SIMPLE_DATE_FORMAT_THREAD_LOCAL;
  }

  public static String format(Date date, ThreadLocal<SimpleDateFormat> simpleDateFormat) {
    return simpleDateFormat.get().format(date);
  }

  public static Date parse(String date) {
    try {
      return DateUtils.getSimpleDateFormatThreadLocal().get().parse(date);
    } catch (Exception e) {
      return null;
    }
  }

  public static Date parse(String date, ThreadLocal<SimpleDateFormat> simpleDateFormat) {
    try {
      return simpleDateFormat.get().parse(date);
    } catch (Exception e) {
      return null;
    }
  }
}
